/*
 * @Copyright: Citylife 2021
 * @Author: shaokui.gu
 * @Date: 2021-02-19 17:19:38
 * @Description: 
 */
import 'dart:ui';

import '../base/base.dart';
import 'gesture_avoid_detector.dart';

enum TableViewCellType {
  /// 默认，左标题右内容
  Default,

  /// 上标题，下副标题
  Subtitle,

  /// 自定义
  Custom,
}

class TableViewCell extends StatelessWidget {
  final String title;
  final String subTitle;
  final String detailText;
  final TextStyle titleStyle;
  final TextStyle subTitleStyle;
  final TextStyle detailTextStyle;
  final Icon icon;
  final Widget rightWidget;
  final Widget child;
  final TableViewCellType type;
  final double height;
  final VoidCallback onTap;
  final Color backgroundColor;
  final bool showSeparator;
  final Color separatorColor;
  final EdgeInsets separatorInset;
  final EdgeInsets contentInset;
  TableViewCell({
    this.title = "",
    this.subTitle = "",
    this.detailText = "",
    this.icon,
    this.backgroundColor,
    this.rightWidget,
    this.child,
    this.titleStyle,
    this.subTitleStyle,
    this.detailTextStyle,
    this.height = 44,
    this.onTap,
    this.type = TableViewCellType.Default,
    this.showSeparator = true,
    this.separatorColor,
    this.contentInset =
        const EdgeInsets.symmetric(vertical: 10, horizontal: 16),
    this.separatorInset = const EdgeInsets.symmetric(horizontal: 16),
  });

  @override
  Widget build(BuildContext context) {
    List<Widget> children = [];
    if (type == TableViewCellType.Custom) {
      children.add(
        Container(
            padding: contentInset,
            color:
                backgroundColor ?? theme.colors.defaultListCellBackgroundColor,
            child: child ?? Container()),
      );
    } else {
      List<Widget> leftWidgets = [];
      List<Widget> rightWidgets = [];
      if (icon != null) {
        leftWidgets.add(icon);
        leftWidgets.add(
          SizedBox(
            width: 8.0.w,
          ),
        );
      }
      leftWidgets.add(
        Text(
          title,
          style: titleStyle,
        ),
      );
      rightWidgets.add(
        SizedBox(
          width: 8.0.w,
        ),
      );
      rightWidgets.add(
        Text(
          detailText,
          style: detailTextStyle,
        ),
      );
      if (rightWidget != null) {
        rightWidgets.add(
          SizedBox(
            width: 8.0.w,
          ),
        );
        rightWidgets.add(rightWidget);
      }

      Widget mainWidget = Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: leftWidgets,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: rightWidgets,
          ),
        ],
      );
      if (type == TableViewCellType.Default) {
        children.add(
          Container(
            padding: contentInset,
            constraints: BoxConstraints(
              minHeight: height,
            ),
            color: theme.colors.defaultListCellBackgroundColor,
            child: mainWidget,
          ),
        );
      } else {
        children.add(
          Container(
            padding: contentInset,
            constraints: BoxConstraints(
              minHeight: height,
            ),
            color: theme.colors.defaultListCellBackgroundColor,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                mainWidget,
                Row(
                  children: [
                    Expanded(
                      child: Text(
                        subTitle,
                        overflow: TextOverflow.ellipsis,
                        style: subTitleStyle,
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        );
      }
    }
    if (showSeparator) {
      children.add(Positioned(
        left: separatorInset.left,
        right: separatorInset.right,
        bottom: 0,
        height: 1.0,
        child: Container(
          color: separatorColor ?? theme.colors.defaultSeparatorColor,
        ),
      ));
    }
    return GestureAvoidDetector(
      onTap: onTap,
      child: Stack(children: children),
    );
  }

  static defaultCell({
    String title,
    String detailText,
    double height,
    Widget rightWidget,
    bool showSeparator = true,
    VoidCallback onTap,
    Color backgroundColor,
  }) {
    return TableViewCell(
      title: title ?? "",
      detailText: detailText ?? "",
      backgroundColor: backgroundColor,
      height: height ?? 44,
      showSeparator: showSeparator,
      titleStyle: TextStyle(
        color: theme.colors.defaultTitleColor,
        fontSize: 14.0.sp,
        fontWeight: FontWeight.normal,
      ),
      detailTextStyle: TextStyle(
        color: theme.colors.defaultDescColor,
        fontSize: 14.0.sp,
        fontWeight: FontWeight.normal,
      ),
      rightWidget: rightWidget,
      onTap: onTap,
    );
  }

  static subTitleCell({
    String title,
    String subTitle,
    String detailText,
    Widget rightWidget,
    double height,
    bool showSeparator = true,
    VoidCallback onTap,
    Color backgroundColor,
  }) {
    return TableViewCell(
      title: title ?? "",
      subTitle: subTitle ?? "",
      detailText: detailText ?? "",
      height: height,
      backgroundColor: backgroundColor,
      showSeparator: showSeparator,
      titleStyle: TextStyle(
        color: theme.colors.defaultTitleColor,
        fontSize: 14.0.sp,
        fontWeight: FontWeight.normal,
      ),
      subTitleStyle: TextStyle(
        color: theme.colors.defaultSubTitleColor,
        fontSize: 14.0.sp,
        fontWeight: FontWeight.normal,
      ),
      detailTextStyle: TextStyle(
        color: theme.colors.defaultDescColor,
        fontSize: 14.0.sp,
        fontWeight: FontWeight.normal,
      ),
      rightWidget: rightWidget,
      type: TableViewCellType.Subtitle,
      onTap: onTap,
    );
  }

  static customCell({
    Widget child,
    bool showSeparator = true,
    Color backgroundColor,
    VoidCallback onTap,
  }) {
    return TableViewCell(
      showSeparator: showSeparator,
      backgroundColor: backgroundColor,
      type: TableViewCellType.Custom,
      onTap: onTap,
      child: child,
    );
  }
}
